home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / import / gradientimport / common / gradientimport.c next >
Encoding:
C/C++ Source or Header  |  1996-09-05  |  12.6 KB  |  603 lines

  1. /*
  2.     File: GradientImport.c
  3.  
  4.     Copyright (c) 1990, Thomas Knoll.
  5.     Copyright (c) 1993-6, Adobe Systems Incorporated.
  6.     All rights reserved.
  7.  
  8.     C source file for import example.
  9. */
  10.  
  11. #if __MWERKS__
  12. #include <SetupA4.h> // A4-globals
  13. #include <A4Stuff.h> // A4-globals
  14. #endif
  15.  
  16. #if defined(THINK_C) || defined(__MWERKS__)
  17. #define ENTRYPOINT main
  18. #endif      
  19.  
  20. #include "GradientImport.h"
  21.  
  22. Handle hDllInstance = NULL;
  23.  
  24. /*****************************************************************************/
  25. void InitGlobals (GPtr globals);
  26. void ValidateParameters (GPtr globals);
  27. void CreateDescriptor (GPtr globals);
  28. void SwitchScriptInfo (GPtr globals, PIReadDescriptor readToken);
  29. void DoPrepare (GPtr globals);
  30. void DoStart (GPtr globals);
  31. void DoContinue (GPtr globals);
  32. void DoFinish (GPtr globals);
  33. void DoFinalize (GPtr globals);
  34. /*****************************************************************************/
  35.  
  36. /* All calls to the plug-in module come through this routine. It must be
  37.    placed first in the resource. To achieve this, most development systems
  38.    require that this be the first routine in the source. */
  39.    
  40. #if MSWindows
  41. void ENTRYPOINT (short selector,
  42.                         AcquireRecord *acquireParamBlock,
  43.                         long *data,
  44.                         short *result)
  45. #else
  46. pascal void ENTRYPOINT (short selector,
  47.                         AcquireRecord *acquireParamBlock,
  48.                         long *data,
  49.                         short *result)
  50. #endif 
  51.     {
  52.     
  53.     /* We copy the globals out of the handle into a stack allocated area
  54.        so that we don't have to worry about the handle moving. */
  55.     
  56.     GPtr globals;
  57.     Globals globalsValue;
  58.  
  59.     #if __MWERKS__
  60.     EnterCodeResource(); // A4-globals
  61.     #endif
  62.         
  63.     if (!*data)
  64.         {
  65.         
  66.         InitGlobals (&globalsValue);
  67.  
  68.         *data = (long) NewHandle (sizeof (Globals));
  69.         
  70.         if (!*data)
  71.             {
  72.             *result = memFullErr;
  73.             return;
  74.             }
  75.             
  76.         ** (GHdl) *data = globalsValue;
  77.         
  78.         }
  79.         
  80.     globalsValue = ** (GHdl) *data;
  81.         
  82.     globals = &globalsValue;
  83.     
  84.     gStuff = acquireParamBlock;
  85.     gResult = noErr;
  86.         
  87.     switch (selector)
  88.         {
  89.         
  90.         case acquireSelectorAbout:
  91.             DoAbout (globals);
  92.             break;
  93.             
  94.         case acquireSelectorPrepare:
  95.             DoPrepare (globals);
  96.             break;
  97.         
  98.         case acquireSelectorStart:
  99.             DoStart (globals);
  100.             break;
  101.         
  102.         case acquireSelectorContinue:
  103.             DoContinue (globals);
  104.             break;
  105.         
  106.         case acquireSelectorFinish:
  107.             DoFinish (globals);
  108.             break;
  109.             
  110.         case acquireSelectorFinalize:
  111.             DoFinalize (globals);
  112.             break;
  113.             
  114.         default:
  115.             gResult = acquireBadParameters;
  116.         
  117.         }
  118.         
  119.     *result = gResult;    
  120.     ** (GHdl) *data = globalsValue;
  121.  
  122.     #if __MWERKS__
  123.     ExitCodeResource(); // A4-globals
  124.     #endif
  125.         
  126.     }
  127.  
  128. /*****************************************************************************/
  129.  
  130. /* We can initialize our globals here, but since we don't know if we'll always
  131.    get this call, it's better to flag them and check that they're valid before
  132.    we first use them. */
  133.  
  134. void InitGlobals (GPtr globals)
  135. {
  136.     
  137.     gInitParameters = true;
  138.     gQueryForParameters = true;
  139.     
  140. }
  141.  
  142. /*****************************************************************************/
  143.  
  144. /* Check parameters and make sure their valid, or initialize them. */
  145.  
  146. void ValidateParameters (GPtr globals)
  147. {
  148.     short loop;
  149.         
  150.     if (gInitParameters)
  151.     {
  152.         gDialog = NULL;
  153.  
  154.         #if Macintosh
  155.             gDialogHdl = NULL;     
  156.         #endif
  157.     
  158.         gLastImages = 0;
  159.         gLastRows = 256;
  160.         gLastCols = 256;
  161.         gLastMode = plugInModeRGBColor;
  162.         gLastInvert = false;
  163.         gContinueImport = false;
  164.         
  165.         gInitParameters = false;
  166.         
  167.         gCount = 0;
  168.         gToken = NULL;
  169.         
  170.         for (loop = 0; loop < kMaxDescriptors; loop++)
  171.             gArray [loop] = NULL;    
  172.     }
  173. }
  174.  
  175. /*****************************************************************************/
  176.  
  177. /* Prepare to acquire an image.    If the plug-in module needs a large amount
  178.    of buffer memory, this routine should set the maxData field to the
  179.    number of bytes required.  Since we are going to use the bufferProcs,
  180.    we simply set maxData to zero.  */
  181.  
  182. void DoPrepare (GPtr globals)
  183.     {
  184.     
  185.     gStuff->maxData = 0;
  186.     
  187.     if (!WarnBufferProcsAvailable ())
  188.         gResult = userCanceledErr; // exit gracefully
  189.         
  190.     /* If finalization is available, we will want it. */
  191.     
  192.     gStuff->wantFinalize = true;
  193.  
  194.     ValidateParameters (globals);
  195.     /* if stuff hasn't been initialized that we need, do it,
  196.         then go check if we've got scripting commands to
  197.         override our settings */
  198.     OpenScriptParams (globals); // update our parameters with the scripting parameters, if available
  199.  
  200. }
  201.  
  202.  
  203. /*****************************************************************************/
  204.  
  205. void DoStart (GPtr globals)
  206. {
  207.     
  208.     int16 j;
  209.  
  210.     /* Insist on having the buffer procs. */
  211.     
  212.     if (!WarnBufferProcsAvailable ())
  213.         {
  214.         gResult = userCanceledErr; // exit gracefully
  215.         return;
  216.         }
  217.         
  218.     /* Assume we won't be coming back around for another pass unless explicitly so. */
  219.         
  220.     gStuff->acquireAgain = gContinueImport = false;
  221.     
  222.     /* If we need to use our dialog, or it's already up, do it. */
  223.     
  224.     ValidateParameters (globals);
  225.     ReadScriptParams (globals);
  226.     
  227.     if (gQueryForParameters)
  228.         {
  229.         /* Open the dialog.  If it is already up, this does nothing.             */
  230.     
  231.         if (!OpenOurDialog (globals))
  232.             {
  233.             gQueryForParameters = false;
  234.             CloseScriptParams(globals);
  235.             gResult = memFullErr;
  236.             return;
  237.             }
  238.  
  239.         /* Run the parameters dialog.                                             */
  240.     
  241.         if (!RunOurDialog (globals))
  242.             {
  243.             
  244.             gQueryForParameters = false;
  245.             
  246.             CloseOurDialog (globals);
  247.             CloseScriptParams(globals);
  248.     
  249.             gResult = userCanceledErr;
  250.             //gStuff->wantFinalize = true;
  251.             return;
  252.                 
  253.             }
  254.         else
  255.             {
  256.  
  257.             /* Still going! */
  258.         
  259.             gContinueImport = true;
  260.         
  261.             }
  262.             
  263.         /* If we cannot finalize, then we want to close the dialog. */
  264.     
  265.         if (!gStuff->canFinalize)
  266.             {
  267.             
  268.             gQueryForParameters = false;
  269.             CloseOurDialog (globals);
  270.             CloseScriptParams(globals);
  271.                 
  272.             }
  273.             
  274.  
  275.         }
  276.                 
  277. /* Fill in the parameters for the document. */
  278.     
  279.     gStuff->imageSize.v = gLastRows;
  280.     gStuff->imageSize.h = gLastCols;
  281.     gStuff->imageMode    = gLastMode;
  282.  
  283.     if (gStuff->imageMode == plugInModeBitmap)
  284.         gStuff->depth = 1;
  285.     else
  286.         gStuff->depth = 8;
  287.  
  288.     if (gStuff->imageMode == plugInModeRGBColor)
  289.         gStuff->planes = 3;
  290.     else
  291.         gStuff->planes = 1;
  292.  
  293.     if (gStuff->depth == 1)
  294.         {
  295.         gStuff->imageHRes = FixRatio (300, 1);
  296.         gStuff->imageVRes = FixRatio (300, 1);
  297.         }
  298.     else
  299.         {
  300.         gStuff->imageHRes = FixRatio (72, 1);
  301.         gStuff->imageVRes = FixRatio (72, 1);
  302.         }
  303.  
  304.     if (gStuff->imageMode == plugInModeIndexedColor)
  305.         for (j = 0; j < 256; j++)
  306.             {
  307.             gStuff->redLUT     [j] = (unsigned8) j;
  308.             gStuff->greenLUT [j] = (unsigned8) (255 - j);
  309.             gStuff->blueLUT  [j] = (unsigned8) j;
  310.             }
  311.         
  312.     gStuff->data = nil;
  313. }
  314.  
  315. /*****************************************************************************/
  316.  
  317. #define RANGE_ITER(lower,upper,first,last,step)                                  \
  318.     for (lower = (first);                                                       \
  319.          (upper = (((lower) + (step) < (last)) ? (lower) + (step) : (last))), \
  320.          lower < (last);                                                      \
  321.          lower = upper)
  322.  
  323. /*****************************************************************************/
  324.  
  325. void DoContinue (GPtr globals)
  326.     {
  327.     
  328.     AcquireRegion region;
  329.     AcquireDataLayout layout;
  330.     Ptr data;
  331.     int32 chunkRows;
  332.     BufferID buffer = 0;
  333.     int16 maxHeight;
  334.     int16 actualHeight;
  335.         
  336.     long done = 0;
  337.     long total = (long) gStuff->imageSize.v * (long) gStuff->planes;
  338.         
  339.     /* Scale total for post-processing. */
  340.     
  341.     if (gLastInvert && gStuff->canReadBack)
  342.         total *= 3;
  343.     
  344.     /* Set up the layout. */
  345.     
  346.     if (gStuff->depth == 8)
  347.         layout.rowBytes = gStuff->imageSize.h;
  348.     else
  349.         layout.rowBytes = (gStuff->imageSize.h + 7) >> 3;
  350.     layout.colBytes = 1;
  351.     layout.planeBytes = 0;    /* irrelevant -- we work one plane at a time */
  352.     
  353.     /* Allocate the buffer and start acquisition.   We limit the buffer
  354.        height to keep the space consumed by this plug-in down.   We base
  355.        this estimate on reserving enough space for one of row of tiles
  356.        with an assumed height of 300 pixels. */
  357.        
  358.     maxHeight = (int16) ((BufferSpace () / layout.rowBytes) - 300);
  359.     
  360.     if (maxHeight > gStuff->imageSize.v)
  361.         maxHeight = gStuff->imageSize.v;
  362.     else if (maxHeight < 1)
  363.         maxHeight = 1;
  364.         
  365.     gResult = AllocateStripBuffer (layout.rowBytes,
  366.                                    1,
  367.                                    maxHeight,
  368.                                    1,
  369.                                    &actualHeight,
  370.                                    &buffer);
  371.     
  372.     chunkRows = actualHeight;                               
  373.     
  374.     if (gResult != noErr)
  375.         goto exit;
  376.             
  377.     data = LockBuffer (buffer, false);
  378.     
  379.     region.rect.left = 0;
  380.     region.rect.right = gStuff->imageSize.h;
  381.     
  382.     for (region.loPlane = 0; region.loPlane < gStuff->planes; ++region.loPlane)
  383.         {
  384.         
  385.         region.hiPlane = region.loPlane;
  386.             
  387.         RANGE_ITER (region.rect.top, region.rect.bottom,
  388.                     0, gStuff->imageSize.v, chunkRows)
  389.             {
  390.             
  391.             Ptr p;
  392.             int16 subRow;
  393.             
  394.             for (subRow = region.rect.top, p = data;
  395.                  subRow < region.rect.bottom;
  396.                  ++subRow, p += layout.rowBytes)
  397.                 {
  398.                 
  399.                 int16 col; 
  400.                 Ptr q;
  401.     
  402.                 if (TestAbort ())
  403.                     {
  404.                     gResult = userCanceledErr;
  405.                     goto exit;
  406.                     }
  407.                     
  408.                 UpdateProgress (done++, total);
  409.                 
  410.                 for (col = 0, q = p; col < layout.rowBytes; ++col, ++q)
  411.                     {
  412.                     
  413.                     if (gStuff->depth == 1)
  414.                         *q = 0xFF << (subRow & 7);
  415.                     else if (gStuff->planes == 1)
  416.                         *q = subRow + col;
  417.                     else if (region.loPlane == 0)
  418.                         *q = (char) col;
  419.                     else if (region.loPlane == 1)
  420.                         *q = (char ) subRow;
  421.                     else
  422.                         *q = subRow + col;
  423.                     
  424.                     }
  425.                     
  426.                 }
  427.             
  428.             gResult = StoreData (gStuff, data, ®ion, &layout);
  429.             
  430.             if (gResult != noErr)
  431.                 goto exit;
  432.             
  433.             }
  434.             
  435.         }
  436.             
  437.     if (gLastInvert && gStuff->canReadBack)
  438.         {
  439.         
  440.         for (region.loPlane = 0; region.loPlane < gStuff->planes; ++region.loPlane)
  441.             {
  442.             
  443.             region.hiPlane = region.loPlane;
  444.                 
  445.             RANGE_ITER (region.rect.top, region.rect.bottom,
  446.                         0, gStuff->imageSize.v, chunkRows)
  447.                 {
  448.                 
  449.                 Ptr p;
  450.                 int16 subRow;
  451.                 
  452.                 gResult = FetchData (gStuff, data, ®ion, &layout);
  453.                 
  454.                 if (gResult != noErr)
  455.                     goto exit;
  456.                 
  457.                 done += region.rect.bottom - region.rect.top;
  458.                 
  459.                 for (subRow = region.rect.top, p = data;
  460.                      subRow < region.rect.bottom;
  461.                      ++subRow, p += layout.rowBytes)
  462.                     {
  463.                     
  464.                     int16 col; 
  465.                     Ptr q;
  466.         
  467.                     if (TestAbort ())
  468.                         {
  469.                         gResult = userCanceledErr;
  470.                         goto exit;
  471.                         }
  472.                         
  473.                     UpdateProgress (done++, total);
  474.                     
  475.                     for (col = 0, q = p; col < layout.rowBytes; ++col, ++q)
  476.                         *q = ~*q;
  477.                     
  478.                     }
  479.                     
  480.                 gResult = StoreData (gStuff, data, ®ion, &layout);
  481.                 
  482.                 if (gResult != noErr)
  483.                     goto exit;
  484.                 
  485.                 }
  486.                 
  487.             }
  488.         
  489.         }
  490.         
  491.     gStuff->data = nil;
  492.     PISetRect (&gStuff->theRect, 0, 0, 0, 0);
  493.     
  494.     exit:
  495.     
  496.     if (buffer != 0)
  497.         FreeBuffer (buffer);
  498.         
  499.     }
  500.  
  501. /*****************************************************************************/
  502.  
  503. /* This routine will always be called if DoStart does not return an error
  504.    (even if DoContinue returns an error or the user aborts the operation).
  505.    This allows the module to perform any needed cleanup. */
  506.  
  507. void DoFinish (GPtr globals)
  508.     {
  509.  
  510.     gStuff->acquireAgain = gContinueImport;
  511.     
  512.     /* Store scripting parameters in object descriptor */
  513.     CreateDescriptor(globals);
  514.     
  515.     /* Write our parameters if we can't finalize */
  516.     
  517.     if (!gStuff->canFinalize)
  518.         CheckAndWriteScriptParams(globals); // writes script params
  519.         
  520.     }
  521.  
  522. /*****************************************************************************/
  523.  
  524. void DoFinalize (GPtr globals)
  525.     {
  526.     
  527.     gQueryForParameters = false;
  528.     CloseOurDialog (globals);
  529.     
  530.     /* Write our parameters */
  531.     
  532.     CheckAndWriteScriptParams(globals); // writes script params
  533.     
  534.     }
  535.  
  536. /*****************************************************************************/
  537.  
  538. OSErr StoreData (AcquireRecord *stuff,
  539.                  void *data,
  540.                  AcquireRegion *region,
  541.                  AcquireDataLayout *layout)
  542.     {
  543.     
  544.     OSErr result = noErr;
  545.     
  546.     if (!WarnHostAdvanceStateAvailable (stuff->advanceState, hDllInstance))
  547.         return userCanceledErr; // exit gracefully
  548.         
  549.     stuff->data = data;
  550.     stuff->theRect = region->rect;
  551.     stuff->loPlane = region->loPlane;
  552.     stuff->hiPlane = region->hiPlane;
  553.     stuff->rowBytes = layout->rowBytes;
  554.     stuff->colBytes = (short)(layout->colBytes);
  555.     stuff->planeBytes = layout->planeBytes;
  556.     
  557.     stuff->wantReadBack = false;
  558.     
  559.     result = (*(stuff->advanceState)) ();
  560.     
  561.     stuff->data = NULL;
  562.     
  563.     return result;
  564.     
  565.     }
  566.     
  567. /*****************************************************************************/
  568.  
  569. OSErr FetchData (AcquireRecord *stuff,
  570.                  void *data,
  571.                  AcquireRegion *region,
  572.                  AcquireDataLayout *layout)
  573.     {
  574.     
  575.     OSErr result = noErr;
  576.     
  577.     if (!WarnHostAdvanceStateAvailable (stuff->advanceState, hDllInstance))
  578.         return userCanceledErr; // exit gracefully
  579.         
  580.     if (!stuff->canReadBack)
  581.         return errPlugInHostInsufficient;
  582.         
  583.     stuff->data = data;
  584.     stuff->theRect = region->rect;
  585.     stuff->loPlane = region->loPlane;
  586.     stuff->hiPlane = region->hiPlane;
  587.     stuff->rowBytes = layout->rowBytes;
  588.     stuff->colBytes = (short)(layout->colBytes);
  589.     stuff->planeBytes = layout->planeBytes;
  590.     
  591.     stuff->wantReadBack = true;
  592.     
  593.     result = (*(stuff->advanceState)) ();
  594.     
  595.     stuff->data = NULL;
  596.     stuff->wantReadBack = false;
  597.     
  598.     return result;
  599.     
  600.     }
  601.  
  602. /*****************************************************************************/
  603.